import {view, View} from "adajs";
import LoadingService from "./state";

@view({
	className: "loading",
	template: "./template.html",
	style: "./style.scss",
	dataset: {
		service: LoadingService
	}
})

class Loading extends View {
	oncreated() {
		setTimeout(() => {
			if (!this.isRemoved()) {
				this.className.add("in");
			}
		}, 100);
	}

	showLoading(content) {
		return this.commit("set", {
			icon: ':for-autorenew',
			circle: true,
			color: "black",
			content: content || "loading..."
		});
	}

	showSuccess(content) {
		return this.commit("set", {
			icon: ':for-check_circle',
			circle: false,
			color: "green",
			content: content || "Success done"
		});
	}

	showError(content) {
		return this.commit("set", {
			icon: ':for-cancel',
			circle: false,
			color: "red",
			content: content || "Error occur"
		});
	}

	close(delay = 2000) {
		return new Promise((resolve) => {
			setTimeout(() => {
				if (!this.isRemoved()) {
					this.className.remove("in");
					setTimeout(() => {
						this.getParent() && this.getParent().removeChild(this);
						resolve();
					}, 400);
				}
			}, delay);
		});
	}
}

export default Loading;